package com.zb.kc.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.kc.commonutils.R;
import com.zb.kc.entity.EduCourse;
import com.zb.kc.entity.EduTeacher;
import com.zb.kc.service.IEduCourseService;
import com.zb.kc.service.IEduTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/eduservice/indexfront")
public class IndexFrontController {

    @Autowired
    private IEduCourseService courseService;

    @Autowired
    private IEduTeacherService teacherService;

    @GetMapping
    public R index() {
        // 前8门课程
        List<EduCourse> courseList = courseService.getHotCourse();
        // 查询前4条讲师
        List<EduTeacher> teacherList = teacherService.getHotTeacher();
        return R.ok().data("courseList", courseList).data("teacherList", teacherList);
    }

    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("teacherPage/{current}/{size}")
    public R getTeacherPageList(@PathVariable("current") long current, @PathVariable("size") long size) {
        Page<EduTeacher> teacherPage = new Page<>(current, size);
        Page<EduTeacher> teaPage = teacherService.page(teacherPage, new QueryWrapper<EduTeacher>().orderByAsc("sort"));
        return R.ok().dataPage(teaPage);
    }

    @ApiOperation(value = "根据讲师id查询讲师信息包括课程信息")
    @GetMapping("getTeacherAndCourse/{id}")
    public R getCourseByTeacherId(@PathVariable("id") String id) {
        EduTeacher teacher = teacherService.getById(id);
        List<EduCourse> courses = courseService.getCourseByTeacherId(id);
        return R.ok().data("teacher", teacher).data("course", courses);
    }
}
